package com.wfy.common.security.shiro;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

/**
 *
 */
public class RedisManager {

    private JedisPool jedisPool;

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    /**
     * get value from redis
     *
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        if (key == null) {
            return null;
        }
        byte[] value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * set
     *
     * @param key
     * @param value
     * @return
     */
    public byte[] set(byte[] key, byte[] value) {
        return set(key, value, 0);
    }

    /**
     * hset
     *
     * @param keyPrefix
     * @param key
     * @param value
     * @return
     */
    public byte[] hset(String keyPrefix, byte[] key, byte[] value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.hset(keyPrefix.getBytes(), key, value);
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * hget
     *
     * @param keyPrefix
     * @param key
     * @return
     */
    public byte[] hget(String keyPrefix, byte[] key) {
        Jedis jedis = jedisPool.getResource();

        byte[] value = null;
        try {
            value = jedis.hget(keyPrefix.getBytes(), key);
        } finally {
            jedis.close();
        }
        return value;
    }

    public void hdel(String keyPrefix, byte[] key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.hdel(keyPrefix.getBytes(), key);
        } finally {
            jedis.close();
        }
    }

    public void hdel(String keyPrefix) {
        hdel(keyPrefix, null);
    }

    /**
     * size
     */
    public Long hsize(String keyPrefix) {
        Long dbSize = 0L;
        Jedis jedis = jedisPool.getResource();
        try {
            dbSize = jedis.hlen(keyPrefix);
        } finally {
            jedis.close();
        }
        return dbSize;
    }

    /**
     * size
     */
    public Set<byte[]> hkeys(String keyPrefix) {
        Set<byte[]> value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.hkeys(keyPrefix.getBytes());
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * size
     */
    public List<byte[]> hvalues(String keyPrefix) {
        List<byte[]> value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.hvals(keyPrefix.getBytes());
        } finally {
            jedis.close();
        }
        return value;
    }


    /**
     * set
     *
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        if (key == null) {
            return null;
        }
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * del
     *
     * @param key
     */
    public void del(byte[] key) {
        if (key == null) {
            return;
        }
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * size
     */
    public Long dbSize() {
        Long dbSize = 0L;
        Jedis jedis = jedisPool.getResource();
        try {
            dbSize = jedis.dbSize();
        } finally {
            jedis.close();
        }
        return dbSize;
    }

    /**
     * keys
     *
     * @param pattern
     * @return
     */
    public Set<byte[]> keys(byte[] pattern) {
        Set<byte[]> keys = null;
        Jedis jedis = jedisPool.getResource();
        try {
            keys = jedis.keys(pattern);
        } finally {
            jedis.close();
        }
        return keys;
    }
}
